package com.deng.framework.utils;

/**
 * class相关的所有自定义工具类
 */
public class ClassUtils {
    public static ClassLoader getDefaultClassLoader(){
        ClassLoader cl = null;
        try {
            cl = Thread.currentThread().getContextClassLoader();
        }
        catch (Throwable ex) {
            // Cannot access thread context ClassLoader - falling back to system class loader...
        }
        if(cl == null){
            // No thread context class loader -> use class loader of this class.
            cl = ClassUtils.class.getClassLoader();
        }
        return cl;
    }

    /**
     * 通过cglib实例化的bean，其getclass方法获取的class是cglib包装过的，
     * 所以对于传进来的bean，要对其实例化方法进行判别，不然会出错，于是就有了这个方法
     */
    public static boolean isCglibProxyClass(Class<?> clazz) {
        return (clazz != null && isCglibProxyClassName(clazz.getName()));
    }

    /**
     * Check whether the specified class name is a CGLIB-generated class.
     * @param className the class name to check
     */
    public static boolean isCglibProxyClassName(String className) {
        return (className != null && className.contains("$$"));
    }

    /**
     * 获取实际的class
     */
    public static Class<?> getActualClass(Class<?> clazz) {
        return isCglibProxyClass(clazz) ? clazz.getSuperclass() : clazz;
    }
}
